package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.wx_user.WXShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "用户购物车接口")
public class shoppingCartController {
    @Autowired
    private WXShoppingCartService wxShoppingCartService;

    /**
     * 添加购物车
     * @param dto
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO dto) {
        log.info("添加购物车:{}", dto);
        wxShoppingCartService.addShoppingCart(dto);
        return Result.success();
    }
    /**
     * 查看购物车
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result list() {
        //只能查看当前用户购物车数据
        log.info("查看购物车");
        List<ShoppingCart> list = wxShoppingCartService.list();
        return Result.success(list);
    }

    /**
     * 清空购物车
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean() {
        log.info("清空购物车");
        wxShoppingCartService.clean();
        return Result.success();
    }
    /**
     * 删除购物车中一个商品
     */
   @ApiOperation("删除购物车中一个商品")
   @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO dto) {
        log.info("删除购物车中一个商品:{}", dto);
        wxShoppingCartService.sub(dto);
        return Result.success();
    }
}
